package com.xzqwjw.taskmanager.dao;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Environment;

public class ItemDataDao {

  public static List<Map<String, Object>> listAll(String strDBFile) {
    Map<String, Object> mapData = null;
    List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

    Map<String, Object> mapPartyInfo = null;
    List<Map<String, Object>> listPartyInfo = null;

    InfoPartyBasic objPartyInfo = null;
    com.leaderinfo.dao.PartyInfoDao objPartyInfoDAO = new com.leaderinfo.dao.PartyInfoDao(strDBFile);

    listPartyInfo = objPartyInfoDAO.getAllRecords();
    for (int i = 0; i < listPartyInfo.size(); i++) {
      mapPartyInfo = listPartyInfo.get(i);
      objPartyInfo = (InfoPartyBasic) mapPartyInfo.get("PartyInfo");
      mapData = setMapData(objPartyInfo, strDBFile);
      listData.add(mapData);
    }
    return listData;
  }

  // public static List<Map<String, Object>> getInfosByUnitID(String
  // strDBFile,
  // int intUnitID) {
  // Map<String, Object> mapData = null;
  // List<Map<String, Object>> listData = new ArrayList<Map<String,
  // Object>>();
  //
  // Map<String, Object> mapPartyInfo = null;
  // List<Map<String, Object>> listPartyInfo = null;
  //
  // InfoPartyBasic objPartyInfo = null;
  // DAOPartyInfo objPartyInfoDAO = new DAOPartyInfo(strDBFile);
  // listPartyInfo = objPartyInfoDAO.getRecordsByUnitID(intUnitID);
  // for (int i = 0; i < listPartyInfo.size(); i++) {
  // mapPartyInfo = listPartyInfo.get(i);
  // objPartyInfo = (InfoPartyBasic) mapPartyInfo.get("PartyInfo");
  // mapData = setMapData(objPartyInfo, strDBFile);
  // listData.add(mapData);
  // }
  //
  // return listData;
  // }

  public static List<Map<String, Object>> getInfosByFavorite(String strDBFile) {
    Map<String, Object> mapData = null;
    List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

    Map<String, Object> mapPartyInfo = null;
    List<Map<String, Object>> listPartyInfo = null;

    InfoPartyBasic objPartyInfo = null;
    com.leaderinfo.dao.PartyInfoDao objPartyInfoDAO = new com.leaderinfo.dao.PartyInfoDao(strDBFile);
    listPartyInfo = objPartyInfoDAO.getRecordsByFavorite();
    for (int i = 0; i < listPartyInfo.size(); i++) {
      mapPartyInfo = listPartyInfo.get(i);
      objPartyInfo = (InfoPartyBasic) mapPartyInfo.get("PartyInfo");
      mapData = setMapData(objPartyInfo, strDBFile);
      listData.add(mapData);
    }

    return listData;
  }

  public static List<Map<String, Object>> getInfosByWhere(String strDBFile,
                                                          String strWhere) {
    Map<String, Object> mapData = null;
    List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

    Map<String, Object> mapPartyInfo = null;
    List<Map<String, Object>> listPartyInfo = null;

    InfoPartyBasic objPartyInfo = null;
    com.leaderinfo.dao.PartyInfoDao objPartyInfoDAO = new com.leaderinfo.dao.PartyInfoDao(strDBFile);
    listPartyInfo = objPartyInfoDAO.getRecordsByWhere(strWhere);
    for (int i = 0; i < listPartyInfo.size(); i++) {
      mapPartyInfo = listPartyInfo.get(i);
      objPartyInfo = (InfoPartyBasic) mapPartyInfo.get("PartyInfo");
      mapData = setMapData(objPartyInfo, strDBFile);
      listData.add(mapData);
    }

    return listData;
  }

  public static List<Map<String, Object>> getInfosByWhere1(String strDBFile,
                                                           String strWhere) {
    Map<String, Object> mapData = null;
    List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

    Map<String, Object> mapPartyInfo = null;
    List<Map<String, Object>> listPartyInfo = null;

    InfoPartyBasic objPartyInfo = null;
    com.leaderinfo.dao.PartyInfoDao objPartyInfoDAO = new com.leaderinfo.dao.PartyInfoDao(strDBFile);
    listPartyInfo = objPartyInfoDAO.getRecordsByWhere1(strWhere);
    for (int i = 0; i < listPartyInfo.size(); i++) {
      mapPartyInfo = listPartyInfo.get(i);
      objPartyInfo = (InfoPartyBasic) mapPartyInfo.get("PartyInfo");
      mapData = setMapData(objPartyInfo, strDBFile);
      listData.add(mapData);
    }

    return listData;
  }

  public static List<Map<String, Object>> getPageData(String strDBFile,
                                                      int intCurrentPage, int intPageSize) {
    Map<String, Object> mapData = null;
    List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

    Map<String, Object> mapPartyInfo = null;
    List<Map<String, Object>> listPartyInfo = null;

    InfoPartyBasic objPartyInfo = null;
    com.leaderinfo.dao.PartyInfoDao objPartyInfoDAO = new com.leaderinfo.dao.PartyInfoDao(strDBFile);

    listPartyInfo = objPartyInfoDAO.getPageRecords(intCurrentPage,
        intPageSize);
    for (int i = 0; i < listPartyInfo.size(); i++) {
      mapPartyInfo = listPartyInfo.get(i);
      objPartyInfo = (InfoPartyBasic) mapPartyInfo.get("PartyInfo");
      mapData = setMapData(objPartyInfo, strDBFile);
      listData.add(mapData);
    }

    return listData;
  }

  // ���������ֱ�ӽ�ͷ����������ݴ浽�ڴ��У�����ķ���ֻ�ǵ�����ͼƬ���ļ���ַ
  @SuppressWarnings("unused")
  private static Map<String, Object> setMapData00(
      InfoPartyBasic objPartyInfo, String strDBFile) {
    Map<String, Object> mapData = null;

    // ͷ�� Photo
    // ���� RealName
    // �Ա� Sex
    // �������£����䣩BirthdayAge
    // ���� NativePlace
    // ��λ��ְ�� UnitJob
    // �Ƿ��ղ� IsFavorite

    Map<String, Object> mapPhoto = null;
    InfoPhoto objPhoto = null;

    int intPartyInfoID = objPartyInfo.AID;
    String strRealName = objPartyInfo.RealName;
    String strBirthday = objPartyInfo.Birthday;
    String strSex = objPartyInfo.Sex;
    String strAge = Utility.getAgeFromBirthday(strBirthday);
    String strNativePlace = objPartyInfo.NativePlace;
    String strIsFavorite = objPartyInfo.IsFavorite + "";

    // �õ�������λ��ְ��ƴ���ַ���
    com.leaderinfo.dao.UnitDao objUnit = new com.leaderinfo.dao.UnitDao(strDBFile);
    String strUnitJob = "";

    strUnitJob = objUnit.getUnitJob(intPartyInfoID);

    // �õ�ͷ�����������
    byte[] aryPhotoData = null;
    com.leaderinfo.dao.PhotoDao objPhotoDAO = new com.leaderinfo.dao.PhotoDao(strDBFile);

    mapPhoto = objPhotoDAO.getRecordByBasicID(intPartyInfoID);

    if (mapPhoto != null) {
      objPhoto = (InfoPhoto) mapPhoto.get("PhotoInfo");
      aryPhotoData = objPhoto.PhotoData;
    }

    // ���ŷ�����"\n"
    mapData = new HashMap<String, Object>();
    mapData.put("AID", "" + intPartyInfoID);
    mapData.put("Photo", Utility.setBytesToBMP(aryPhotoData, null));
    mapData.put("RealName", strRealName);
    mapData.put("Sex", strSex);
    mapData.put("BirthdayAge", strBirthday + " ��" + strAge + "��");
    mapData.put("NativePlace", strNativePlace);
    mapData.put("UnitJob", strUnitJob);
    mapData.put("IsFavorite", strIsFavorite);

    return mapData;
  }

  private static Map<String, Object> setMapData(InfoPartyBasic objPartyInfo,
                                                String strDBFile) {
    Map<String, Object> mapData = null;

    // ͷ���ַPhoto
    // ���� RealName
    // �Ա� Sex
    // �������£����䣩BirthdayAge
    // ���� NativePlace
    // ��λ��ְ�� UnitJob
    // �Ƿ��ղ� IsFavorite

    int intPartyInfoID = objPartyInfo.AID;
    String strIDCardNumber = objPartyInfo.CardNumberID;
    String strRealName = objPartyInfo.RealName;
    String strBirthday = objPartyInfo.Birthday;
    String strSex = objPartyInfo.Sex;
    String strAge = Utility.getAgeFromBirthday(strBirthday);
    String strNativePlace = objPartyInfo.NativePlace;
    String strIsFavorite = objPartyInfo.IsFavorite + "";

    // �õ�������λ��ְ��ƴ���ַ���
    com.leaderinfo.dao.UnitDao objUnit = new com.leaderinfo.dao.UnitDao(strDBFile);
    String strUnitJob = "";

    strUnitJob = objUnit.getUnitJob(intPartyInfoID);

    // ������ݺ���õ�ͷ���ַ

    String strPhotoPath = Environment.getExternalStorageDirectory()
        + File.separator + "PartyInfo" + File.separator + "HeadPhoto"
        + File.separator + strIDCardNumber + ".jpg";

    // ���ŷ�����"\n"
    mapData = new HashMap<String, Object>();
    mapData.put("AID", "" + intPartyInfoID);
    mapData.put("Photo", strPhotoPath);
    mapData.put("RealName", strRealName);
    mapData.put("Sex", strSex);
    mapData.put("BirthdayAge", strBirthday + " ��" + strAge + "��");
    mapData.put("NativePlace", strNativePlace);
    mapData.put("UnitJob", strUnitJob);
    mapData.put("IsFavorite", strIsFavorite);

    return mapData;
  }

}
